package com.hdy.common.network;

import android.content.Context;
import android.net.wifi.ScanResult;

import com.hdy.common.ThreadPoolUtil;
import com.hdy.common.log.LLog;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class AutoConnectNetwork {

    private final static int SCAN_TIME = 1000;
    private final static int SLEEP_TIME = 60000;

    private Context mContext;
    private boolean isNeedConnectAlways;
    HdyWifiManager mHdyWifiManager;

    private final static String HDY_SSID = "hdy";
    private final String mPassword = "hdy00001";


    public AutoConnectNetwork(Context context, boolean isNeedConnectAlways) {
        this.mContext = context;
        this.isNeedConnectAlways = isNeedConnectAlways;

        mHdyWifiManager = new HdyWifiManager(mContext);
    }

    public void onStart() {
        NetworkConnectionManager.getInstance().addNetworkConnectionObserver(mNetworkAvailableObserver);
    }

    public void onDestroy() {
        NetworkConnectionManager.getInstance().removeNetworkConnectionObserver(mNetworkAvailableObserver);
    }

    private INetworkConnectionObserver mNetworkAvailableObserver = new INetworkConnectionObserver() {
        @Override
        public void onAvailable() {
        }

        @Override
        public synchronized void onDisconnected() {
            mHdyWifiManager.startScan();
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    if (isNeedConnectAlways) {
                        while (isNeedConnectAlways) {
                            if (NetworkUtils.isAvailable(mContext) || NetworkUtils.isMobileAvailable(mContext)) {
                                break;
                            }
                            try {
                                autoConnect();
                                Thread.sleep(SLEEP_TIME);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    } else {
                        autoConnect();
                    }
                }
            };
            ThreadPoolUtil.postDelayed(runnable, SCAN_TIME);
        }

        @Override
        public void onNetworkConnectChange(NetworkConnectState state) {

        }
    };

    private void autoConnect() {
        List<ScanResult> hdySSIDList = new ArrayList<>();
        List<ScanResult> scanResults = mHdyWifiManager.getWifiList();
        for(ScanResult result : scanResults) {
            if (result.SSID != null
                    && result.SSID.toLowerCase().startsWith(HDY_SSID)) {
                hdySSIDList.add(result);
            }
        }

        if (hdySSIDList.size() > 0) {
            //按信号强弱排序
            Collections.sort(hdySSIDList, new Comparator<ScanResult>() {
                @Override
                public int compare(ScanResult o1, ScanResult o2) {
                    int i = o2.level - o1.level;
                    if(i == 0){
                        return 0;
                    }
                    return i;
                }
            });

            LLog.i("autoConnect " + hdySSIDList.get(0).SSID);
            mHdyWifiManager.connectWifi(hdySSIDList.get(0), mPassword);
        }

        LLog.i("autoConnect end");
    }
}
